//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/16.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

class StatusCell: UITableViewCell {

    var statusFrame: StatusFrame? {
        didSet{
        // 设置控件大小,以及frame
            setStatuFrame()
        }
    }
    
    // 添加子控件
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 添加原创微博的控件
        contentView.addSubview(originalView)
        //添加转发微博的控件
        contentView.addSubview(retweetView)
        // 添加底部按钮
        contentView.addSubview(statusToolBar)
    }
    
    // 设置控件大小,以及frame
    private func setStatuFrame() {
        
        
        let statusF = statusFrame!
        let status = statusF.status!
        
        // 设置头像
        headImageView.frame = statusF.headImageViewF!
        headImageView.sd_setImageWithURL(NSURL(string: status.user!.profile_image_url!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        // 设置名称
        nameLabel.frame = statusF.nameLabelF!
        nameLabel.text = status.user?.name
        
        // 判断是否是会员
        if status.user!.isVip {
        
            vipImageView.hidden = false
            vipImageView.frame = statusF.vipImageViewF!
            vipImageView.image = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
            nameLabel.textColor = UIColor.orangeColor()
        }else {
            
            vipImageView.hidden = true
            nameLabel.textColor = UIColor.blackColor()
        }
        
        // 设置时间
        let creatAt = status.createdAtStr
        createdTimeLabel.frame = statusF.createdTimeLabelF!
        createdTimeLabel.width = creatAt.size(createdTimeLabel.font).width
        createdTimeLabel.text = creatAt
        
        // 来源
        sourceLabel.frame = statusF.sourceLabelF!
        sourceLabel.x = CGRectGetMaxX(createdTimeLabel.frame) + CELL_CHILD_VIEW_MARGIN
        sourceLabel.text = status.source
        
        // 原创微博内容
        originalTextLabel.frame = statusF.originalTextLabelF!
        originalTextLabel.text = status.text
        
        // 如果有原创微博的配图
        if let originalPicUrls = status.pic_urls where originalPicUrls.count>0 {
            originalPhotos.hidden = false
            originalPhotos.frame = statusF.originalPhotosF!
            originalPhotos.pic_urls = originalPicUrls
        
        }else {
            originalPhotos.hidden = true
        }
        
        // 判断是否有转发微博
        if let retStatus = status.retweeted_status {
            
            // 设置转发微博View大小
            retweetView.hidden = false
            retweetView.frame = statusF.retweetViewF!
            
            // 转发微博内容及大小
            retweetTextLabel.frame = statusF.retweetTextLabelF!
            retweetTextLabel.text = retStatus.text
            
            // 判断是否有转发微博图片
            if let retweetPicUrls = retStatus.pic_urls where retweetPicUrls.count > 0 {
            
                retweetPhotos.hidden = false
                retweetPhotos.frame = statusF.retweetPhotosF!
                retweetPhotos.pic_urls = retweetPicUrls
                
            }else {
                retweetPhotos.hidden = true
            }
        }else {
            
            retweetView.hidden = true
        }
        
        // 底部按钮
        statusToolBar.frame = statusF.statusToolBarF!
        statusToolBar.status = status
    }
    
    // MARK: - 设置原创微博
    
    // 原创微博的整体View 
    private lazy var originalView: UIView = {
       
        let view = UIView()
        
        view.addSubview(self.headImageView)
        view.addSubview(self.nameLabel)
        view.addSubview(self.createdTimeLabel)
        view.addSubview(self.sourceLabel)
        view.addSubview(self.originalTextLabel)
        view.addSubview(self.vipImageView)
        view.addSubview(self.originalPhotos)
        
        return view
    }()
 
    /// 原创微博的头像
    private lazy var headImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.layer.borderColor = UIColor.lightGrayColor().CGColor
        imageView.layer.borderWidth = 0.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 名称
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(CELL_STATUS_NAME_FONT)
        
        return label
    }()
    
    /// 会员图标
    private lazy var vipImageView: UIImageView = UIImageView()
    
    /// 发表时间
    private lazy var createdTimeLabel: UILabel = {
    
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(CELL_STATUS_CREATE_AT_FONT)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
    
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(CELL_STATUS_CREATE_AT_FONT)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    /// 原创微博内容的text
    private lazy var originalTextLabel: UILabel = {
        
        let label = UILabel()
        
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT)
        
        return label
    }()
    
    /// 原创微博配图
    private lazy var originalPhotos: StatusPhotos = StatusPhotos()
    
    
    // MARK: - 转发微博整体View
    
    private lazy var retweetView: UIView = {
        
        let view = UIView()
        
        view.backgroundColor = RGB(r: 244, g: 244, b: 244)
        
        view.addSubview(self.retweetTextLabel)
        view.addSubview(self.retweetPhotos)
        
        return view
    }()
    
    // 转发微博内容
    private lazy var retweetTextLabel: UILabel = {
    
        let label = UILabel()
        
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT)
        
        return label
    }()
    
    // 转发微博的配图
    private lazy var retweetPhotos: StatusPhotos = StatusPhotos()
    
    //MARK: - 底部的toolBar
    private lazy var statusToolBar: StatusToolBar = StatusToolBar()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
